package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.RedPacketDao;
import com.baichang.taoshunda.entity.RedPacket;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2018/4/26.
 */
@Repository
public class RedPacketDaoImpl extends BaseDaoImpl<RedPacket> implements RedPacketDao {
    /**
     * 修改红包状态
     *
     * @param redPacketId
     * @return
     */
    @Override
    public boolean updateRedPacketStatus(Integer redPacketId) {
        String sql = "UPDATE `t_red_packet` SET `status`='1' WHERE id=?";
        return update(sql, redPacketId) > 0;
    }

    /**
     * 查询所有的红包
     *
     * @param nowPage
     * @param pageSize
     * @param userId
     * @param cityId
     * @return
     */
    @Override
    public List<Map<String, Object>> findRedPacketList(Integer nowPage, Integer pageSize, Integer userId, Integer cityId) {
        String sql = "SELECT rp.id, rp.all_money, rp.company_id, rp.created, rp.all_number ,(SELECT COUNT(0) FROM " +
                " t_red_packet_history rph WHERE rp.id=rph.red_id AND user_id=?) isHave, rp.number, rp.time_length, rp.content, " +
                "rp.is_complete, rp.pay_type , b.store_pic companyHeadPic, b.buss_name companyName, rp.`status` FROM t_red_packet rp " +
                "INNER JOIN t_bussiness b ON b.id = rp.company_id WHERE rp.`status` = 1 and rp.city_id=? or rp.company_id=91 order by rp.id desc";
        return listForMapList(sql, nowPage, pageSize, userId, cityId);
    }

    @Override
    public boolean updateRedPacketIsCompleteAndNumber(Integer redPacketId, int number, int isComplete, int timeLength) {
        String sql = "UPDATE `t_red_packet` SET `number`=?, `time_length`=?, `is_complete`=? WHERE id=?";
        return update(sql, number, timeLength, isComplete, redPacketId) > 0;
    }

    @Override
    public List<Map<String, Object>> findRedPacketHistory(Integer companyId, Integer nowPage, Integer pageSize) {
        String sql = "SELECT r.id, r.all_money, r.company_id, r.created, r.all_number , r.number, r.pay_type, r.content," +
                " r.is_complete, r.`status` , r.city_id FROM t_red_packet r WHERE r.company_id = ? AND r.`status` = 1 ORDER BY id DESC";
        return listForMapList(sql, nowPage, pageSize, companyId);
    }
}
